package com.purvar.boot.auto.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.purvar.boot.api.entity.SysAutocodeMain;
import com.purvar.boot.api.pojo.SysAutoCodeMainAndDetails;
import com.purvar.boot.api.pojo.SysAutocodeMainEditVo;
import com.purvar.boot.api.pojo.SysAutocodeMainQueryVo;
import com.purvar.boot.api.service.ISysAutocodeMainService;
import com.purvar.boot.comm.api.vo.Result;
import com.purvar.boot.comm.core.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping({"/auto/"})
@Api(
        tags = {"管理"}
)
public class SysAutocodeMainController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(SysAutocodeMainController.class);
    @Autowired
    private ISysAutocodeMainService iSysAutocodeMainService;

    public SysAutocodeMainController() {
    }

    @ApiOperation("新增()数据")
    @PostMapping({"/sysAutocodeMain/createSysAutocodeMain"})
    public Result addSysAutocodeMain(@RequestBody SysAutoCodeMainAndDetails entity) {
        this.iSysAutocodeMainService.addSysAutoCodeMainAndDetails(entity);
        return Result.ok();
    }

    @GetMapping({"/sysAutocodeMain/listSysAutocodeMain"})
    @ApiOperation(
            value = "获取()数据列表",
            notes = "获取()数据列表"
    )
    public Result<IPage<SysAutocodeMain>> listPage(SysAutocodeMainQueryVo sysAutocodeMainQueryVo, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        return this.iSysAutocodeMainService.listPage(sysAutocodeMainQueryVo, pageNo, pageSize, req);
    }

    @GetMapping({"/sysAutocodeMain/getSysAutocodeMain"})
    @ApiOperation(
            value = "根据主键获取数据",
            notes = "根据登主键获取数据"
    )
    public Result getById(@RequestParam(name = "id") String id) {
        Result result = new Result();
        SysAutocodeMain sysAutocodeMain = (SysAutocodeMain) this.iSysAutocodeMainService.getById(id);
        result.setSuccess(true);
        result.setResult(sysAutocodeMain);
        return result;
    }

    @PostMapping({"/sysAutocodeMain/updateSysAutocodeMain"})
    @ApiOperation(
            value = "根据主键更新()数据",
            notes = "根据主键更新()数据"
    )
    public Result updateSysAutocodeMain(@RequestParam(name = "id") String id, @Validated @RequestBody SysAutocodeMainEditVo sysAutocodeMainEditDto) {
        Result result = new Result();
        this.iSysAutocodeMainService.updateSysAutocodeMain(id, sysAutocodeMainEditDto);
        result.setSuccess(true);
        result.setMessage("comm.modifySucc");
        return result;
    }

    @PostMapping({"/sysAutocodeMain/deleteSysAutocodeMain"})
    @ApiOperation(
            value = "根据主键删除()数据",
            notes = "根据主键删除()数据"
    )
    public Result delSysAutocodeMain(@RequestParam(name = "id") String id) {
        Result result = new Result();
        this.iSysAutocodeMainService.removeById(id);
        return result;
    }

    @GetMapping({"/sysAutocodeMain/getByTableName"})
    @ApiOperation(
            value = "根据表名获取数据",
            notes = "根据表名获取数据"
    )
    public Result getByTableName(@RequestParam(name = "tableName") String tableName) {
        return Result.ok(this.iSysAutocodeMainService.getSysAutoCodeMainAndDetailsBytableName(tableName));
    }

    @GetMapping({"/sysAutocodeMain/getEditByTableName"})
    @ApiOperation(
            value = "根据表名获取数据",
            notes = "根据表名获取数据"
    )
    public Result getEditByTableName(@RequestParam(name = "tableName") String tableName) {
        return Result.ok(this.iSysAutocodeMainService.getEditSysAutoCodeMainAndDetailsBytableName(tableName));
    }

    @GetMapping({"/sysAutocodeMain/AutocodeMain"})
    @ApiOperation(
            value = "根据主键生成代码",
            notes = "根据主键生成代码"
    )
    public void AutocodeMain(@RequestParam(name = "id") String id, HttpServletResponse response) {
        new Result();
        byte[] data = this.iSysAutocodeMainService.autocodeMain(id);
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"purchaser.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        try {
            IOUtils.write(data, response.getOutputStream());
        } catch (IOException var6) {
            var6.printStackTrace();
        }

    }
}
